package com.link2room.blaster.facade.impl;

import com.link2room.blaster.facade.IInmailReceiverFacadeService;
import com.link2room.blaster.service.InmailReceiverService;
import com.link2room.common.constant.DefaultValue;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.common.util.ClassConverUtil;
import com.link2room.common.util.L2RAssert;
import com.link2room.blaster.dto.inMail.InmailReceiverDto;
import com.link2room.blaster.dto.param.inMail.InmailReceiverCreateParamDto;
import com.link2room.blaster.dto.param.inMail.InmailReceiverListParamDto;
import com.link2room.blaster.entity.InMailReceiverEntity;
import com.link2room.sideswipe.sideswipe.dto.base.UserDto;
import com.link2room.sideswipe.sideswipe.dto.param.UserParamDto;
import com.link2room.sideswipe.sideswipeClient.sdk.ISideswipeClientAPIService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by LN on 2017/6/29.14:09
 */
@Component("inmailReceiverFacadeService")
public class InmailReceiverFacadeServiceImpl implements IInmailReceiverFacadeService {
    @Resource
    private ICommonService commonService;
    @Resource
    private ISideswipeClientAPIService sideswipeClientAPIService;
    @Resource
    private InmailReceiverService inmailReceiverService;
    @Override
    public void update(InmailReceiverCreateParamDto paramDto) throws Exception {
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode,"[lhotelGroupCode] is null.");
        String lhotelCode = paramDto.getLhotelCode();
        L2RAssert.stringBlank(lhotelCode,"[lhotelCode] is null.");
        String type = paramDto.getInmalType();
        L2RAssert.stringBlank(type,"[type] is null.");
        List<InmailReceiverDto> inmailReceiverDtos = paramDto.getInmailReceiverList();
        if(CollectionUtils.isEmpty(inmailReceiverDtos)){
            return;
        }

        List<String> toDel = inmailReceiverDtos.stream()
                .filter(dto -> DefaultValue.F.equals(dto.getIsHalt()))
                .map(dto->dto.getReceiverCode())
                .collect(Collectors.toList());
        List<String> toSave = inmailReceiverDtos.stream()
                .filter(dto->dto.getId() == null)
                .filter(dto->DefaultValue.T.equals(dto.getIsHalt()))
                .map(dto->dto.getReceiverCode())
                .collect(Collectors.toList());
        inmailReceiverService.updateInmailReceiver(lhotelGroupCode,lhotelCode,type,toDel,toSave);

    }

    @Override
    public List<InmailReceiverDto> inmailReceiverList(InmailReceiverListParamDto paramDto) throws Exception {
        String lhotelGroupCode = paramDto.getLhotelGroupCode();
        L2RAssert.stringBlank(lhotelGroupCode,"[lhotelGroupCode] is null.");
        String lhotelCode = paramDto.getLhotelCode();
        L2RAssert.stringBlank(lhotelCode,"[lhotelCode] is null.");
        String type = paramDto.getInmailType();
        L2RAssert.stringBlank(type,"[type] is null.");
        InMailReceiverEntity find = new InMailReceiverEntity();
        find.setLhotelCode(lhotelCode);
        find.setLhotelGroupCode(lhotelGroupCode);
        find.setInmailType(type);
        QueryResult<InMailReceiverEntity> qres = commonService.findAllEQ(InMailReceiverEntity.class, find, paramDto.getCurrentPage(), paramDto.getPageSize());
        if (qres == null || CollectionUtils.isEmpty(qres.getResultlist())) {
            return Collections.emptyList();
        }else{
            List<String> userCodes = qres.getResultlist()
                    .stream().map(inMailReceiverEntity -> inMailReceiverEntity.getReceiverCode())
                    .collect(Collectors.toList());
            UserParamDto findUser = new UserParamDto();
            findUser.setLhotelGroupCode(lhotelGroupCode);
            findUser.setUserCodes(userCodes);
            List<UserDto> userList = sideswipeClientAPIService.findUsers(findUser);
            List<InmailReceiverDto> receiverlist = qres.getResultlist().stream()
                    .map(inMailReceiverEntity -> ClassConverUtil.copyProperties(inMailReceiverEntity, InmailReceiverDto.class))
                    .collect(Collectors.toList());
            receiverlist.forEach(dto->dto.setUserName(matchUserName(dto.getReceiverCode(),userList)));
            return receiverlist;
        }
    }

    private String matchUserName( String userCode ,  List<UserDto> userList ){
        if(CollectionUtils.isEmpty(userList)){
            return null;
        }else {
            UserDto user = userList.stream()
                    .filter(userDto -> userCode.equals(userDto.getCode()))
                    .findFirst()
                    .orElseGet(null);
            if(user!=null){
                return user.getName();
            }else {
                return null;
            }
        }
    }
}
